package cn.tta.service.impl;

import cn.tta.common.constant.StatusConstant;
import cn.tta.mapper.DishMapper;
import cn.tta.mapper.OrdersMapper;
import cn.tta.mapper.SetmealMapper;
import cn.tta.mapper.UserMapper;
import cn.tta.pojo.entity.Orders;
import cn.tta.pojo.vo.BusinessDataVO;
import cn.tta.pojo.vo.DishOverViewVO;
import cn.tta.pojo.vo.OrderOverViewVO;
import cn.tta.pojo.vo.SetmealOverViewVO;
import cn.tta.service.WorkspaceService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {


    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {

        HashMap<Object, Object> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        // 查询总订单数
        Integer totalOrderCount = ordersMapper.countByMap(map);

        map.put("status", Orders.COMPLETED);

        // 营业额
        Double turnover = ordersMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 :turnover;

        // 有效订单数
        Integer validOrderCount = ordersMapper.countByMap(map);

        Double unitPrice = 0.0;

        Double orderCompletionRate = 0.0;
        if (totalOrderCount !=0 && validOrderCount != 0) {
            // 订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            // 平均客单价
            unitPrice = turnover / validOrderCount;
        }

        // 新增用户数
        Integer newUsers = userMapper.countByMap(map);
        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(Double.valueOf(String.format("%.2f",unitPrice)))
                .newUsers(newUsers).build();
    }

    @Override
    public OrderOverViewVO orderOverView() {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("begin",LocalDateTime.now().with(LocalTime.MIN));
        map.put("status",Orders.TO_BE_CONFIRMED);
        //待接单
        Integer waitingOrders = ordersMapper.countByMap(map);

        // 待派送
        map.put("status",Orders.CONFIRMED);
        Integer deliveredOrders = ordersMapper.countByMap(map);

        //已完成
        map.put("status",Orders.COMPLETED);
        Integer copmletedOrders = ordersMapper.countByMap(map);

        //已取消
        map.put("status",Orders.CANCELLED);
        Integer cancledOrders = ordersMapper.countByMap(map);

        //全部订单
        map.put("status", null);
        Integer allOrders = ordersMapper.countByMap(map);
        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(copmletedOrders)
                .cancelledOrders(cancledOrders)
                .allOrders(allOrders).build();
    }

    @Override
    public DishOverViewVO getDishOverView() {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);
        map.put("status",StatusConstant.DISABLE);
        Integer discountinued = dishMapper.countByMap(map);
        return DishOverViewVO.builder()
                .discontinued(discountinued)
                .sold(sold).build();
    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        HashMap<Object, Object> map = new HashMap<>();
        map.put("status",StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);
        map.put("status",StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold).build();
    }
}
